package com.example.demo.controller;

import com.example.demo.DTO.OrderItemDTO;
import com.example.demo.pojo.OrderItem;
import com.example.demo.pojo.Result;
import com.example.demo.request.CreateOrderRequest;
import com.example.demo.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 描述：订单Controller
 */
@RestController
@RequestMapping("/order")
@Validated
public class OrderController {
	@Autowired
	IOrderService orderService;

	@PostMapping("/create")
	public Result createOrder(@RequestBody CreateOrderRequest createOrderRequest) {
		String orderNum = orderService.create(createOrderRequest);
		return Result.success(orderNum);
	}

	@PostMapping("/cancel")
	public Result cancel(@RequestParam("orderNum") String orderNum) {
		orderService.cancel(orderNum);
		return Result.success();
	}

	@PostMapping("/qrcode")
	public Result qrcode(@RequestParam("orderNo") String orderNum) {
		String pngAddress = orderService.qrcode(orderNum);
		return Result.success(pngAddress);
	}

	@GetMapping("/pay")
	public Result pay(@RequestParam("orderNo") String orderNum) {
		orderService.pay(orderNum);
		return Result.success();
	}
	@GetMapping("/confirm")
	public Result confirm(@RequestParam("orderNo") String orderNum) {
		orderService.confirmOrder(orderNum);
		return Result.success();
	}
	@GetMapping("/user/{userID}")
	public Result<List<OrderItemDTO>> getOrderItemsGroupedBySeller(@PathVariable Integer userID) {
		List<OrderItemDTO> orderItems = orderService.getOrderItemsGroupedBySeller(userID);
		return Result.success(orderItems);
	}

}
